import Mock, { mock } from "mockjs";
// 模拟api接口
var people_list = {};
//生成数据
if (localStorage.getItem('people_list')) {
    people_list = JSON.parse(localStorage.getItem('people_list'))
} else {
    //生成数据
    people_list = Mock.mock({
        "list|20": [
            {
                id: '@id',
                "Num|+1": 10001,
                name: '@cname',
                'job|1': ['测试经理', '产品经理', '测试专员', '产品助理', '产品总监', '技术经理', 'java开发', 'python开发', '前端开发'],
                'phone|13000000000-19999999999': 1,
                'from|1': ['BOSS直聘', '猎聘', '猎头', '前程无忧', '内推', '智联招聘'],
                'status|1': ['录用', '不合格', '持续沟通'],
                'peopleStatus|1': ['跟进中', '已完成'],
                'school|1': ['西安交通大学', '西安石油大学', '西北工业大学', '电子科技大学', '西安科技大学', '兰州大学', '重庆邮电大学', '北京理工大学'],
                'education|1': ['研究生', '本科',],
                email: '@email',
                'jobTime|1': ['2018-3月-2019-3月', '2019-3月-2019-5月', '2019-3月-2020-7月', '2017-6月-2019-12月'],
                'jobaddress|1': ['京东', '阿里集团', '字节跳动', '腾讯', '华为', '小米', '美团', '饿了么'],
                'jobcont|1': ['软件开发'],
                'money|1': ['10k', '11k', '12k', '22k', '10.5k', '9k', '13k', '14k', '21k'],
                'reason|1': ['个人原因', '家庭原因', '其他']
            }
        ]
    })
    localStorage.setItem('people_list', JSON.stringify(people_list))
}
//查询接口
Mock.mock('/api/people/list', 'get', () => {
    //返回数据
    return people_list;
})
//删除接口
Mock.mock('/api/user/delete', 'post', (req) => {
    //接收参数，req.body是字符串格式，需要转为json对象
    var { id } = JSON.parse(req.body);
    var index = people_list.list.findIndex((item) => {
        return item.id == id;
    })
    if (index != -1) {
        people_list.list.splice(index, 1);
        localStorage.setItem('people_list', JSON.stringify(people_list))
    }
    return { status: 200, msg: '删除成功', list: people_list.list }
})
//详情页接口
Mock.mock('/api/user/info', 'post', (req) => {
    var { Num } = JSON.parse(req.body);
    var i = people_list.list.findIndex((item) => {
        return item.Num == Num;
    })
    if (i != -1) {
        return people_list.list[i];
    }

})
// 编辑数据接口
Mock.mock('/api/user/change', 'post', (req) => {
    var { id, Num, name, job, phone, from, status, peopleStatus, school, education, email, jobTime, jobaddress, jobcont, money, reason } = JSON.parse(req.body);
    console.log(id, name, school, education, phone, job);
    let i = people_list.list.findIndex((item) => {
        return item.id == id;
    })
    if (i != 1) {
        people_list.list.splice(i, 1, { id, Num, name, job, phone, from, status, peopleStatus, school, education, email, jobTime, jobaddress, jobcont, money, reason })
        localStorage.setItem('people_list', JSON.stringify(people_list))
    }
})
//新增数据接口
Mock.mock('/api/user/add', 'post', (req) => {

    let { name, job, phone, education, from, status, peopleStatus, email, school, jobTime, jobaddress, jobcont, money, reason } = JSON.parse(req.body);
    let idarr = [];
    people_list.list.map((item) => {
        idarr.push(item.id);
    })
    people_list.list.unshift({ name, job, phone, education, from, status, peopleStatus, email, school, jobTime, jobaddress, jobcont, money, reason })
    localStorage.setItem('people_list', JSON.stringify(people_list))
})
//搜索接口
Mock.mock('/api/user/search', 'post', (req) => {
    var { name } = JSON.parse(req.body);
    var obj = people_list.list.find((item) => {
        return item.name == name
    })
    if (obj) {
        return { code: 1, msg: '查询成功', obj }
    } else {
        return { code: 0, msg: '查询失败' }
    }
})
